package com.ethank.controller.ocr;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ethank.service.impl.OcrServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;

/**
 * @author gouqizi
 * @desc
 * @date 2025/4/22 16:20
 */
public class BaseController {
    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    /**
     * @Description: 校验签名
     * @Param: [shopId, sign]
     * @return: boolean
     * @Author: tf
     * @Date: 2024-10-28
     */
    boolean checkSign(String shopId, String sign) {

        DateTime now = DateUtil.date();
        String nowStr = DateUtil.format(now, "yyyyMMddHHmm");
        boolean flag = false;
        try {
            String token = DigestUtils.md5DigestAsHex((nowStr + shopId).getBytes("UTF-8"));
            logger.info("Info=>checkSign=nowStr={}==sign={}", nowStr + shopId, token);

            if (token.equals(sign)) {
                flag = true;
            }
            if (!flag) {
                // 时间分钟减1
                DateTime subNow = DateUtil.offsetMinute(now, -1);
                String nowSubStr = DateUtil.format(subNow, "yyyyMMddHHmm");
                String newToken = DigestUtils.md5DigestAsHex((nowSubStr + shopId).getBytes());
                logger.info("Info=>checkSign=nowStr={}==sign={}", nowStr + shopId, token);
                if (newToken.equals(sign)) {
                    flag = true;
                }
            }

        } catch (Exception e) {
            logger.error("签名数据校验失败");
        }
        return flag;
    }
}
